/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package construtora.limiar.dao;

import construtora.limiar.dao.generic.AbstractDAO;
import construtora.limiar.dao.generic.DAOEngine;
import construtora.limiar.dao.generic.IDAO;
import construtora.limiar.models.Telefone;
import construtora.limiar.models.generic.IModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author michel
 */
public class TelefoneDAO extends AbstractDAO implements IDAO{

    public TelefoneDAO() {
        this.table = "telefone";
    }
    
    

    @Override
    public String[][] getFields(IModel model) {
        Telefone telefone = (Telefone) model;
        String[][] fields = new String[3][2];
        fields[0][0] = "telefone";
        fields[0][1] = "'" + telefone.getTelefone() + "'";
        fields[1][0] = "tipo";
        fields[1][1] = "'" + telefone.getTipo() + "'";
        fields[2][0] = "status";
        fields[2][1] = "'" + telefone.getStatus() + "'";
        return fields;
    }

    @Override
    public IModel fetchOne(IModel model) {
        String where = this.getDefaultWhere(model);
        List<Telefone> list = (List<Telefone>) this.fetchAll(where, null, null, null);
        if (list != null && list.size() > 0)
            return list.get(0);
        else
            return null;
    }

    @Override
    public List<? extends IModel> fetchAll(String where, String order, Integer limit, Integer offset) {
        List<Telefone> list = new ArrayList<Telefone>();
        ResultSet rs = DAOEngine.fetchAll(this.table, where, order, limit, offset);
        try {
            while (rs.next()) {
                Telefone telefone = new Telefone();
                telefone.setId(rs.getInt("id_telefone"));
                telefone.setTelefone(rs.getString("numero"));
                telefone.setTipo(rs.getString("tipo"));
                telefone.setStatus("status");
                list.add(telefone);
            }
            return list;
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
        
    }
    
    @Override
    public String getDefaultWhere(IModel model) {
        return "id_telefone = " + ((Telefone) model).getId();
    }

    
}
